package com.leetcode.studyplan.algorithm.introduction.thirdday;

public class MoveZeroes {

    public void moveZeroes(int[] nums) {

        if (nums == null || nums.length <= 1) {
            return;
        }

        int left = 0, right = 0;
        while (left < nums.length - 1 && right < nums.length) {

            if (nums[left] != 0) {
                left++; // 左指针指向零
                continue;
            }

            if (right <= left) {
                right = left + 1;
            }

            if (nums[right] == 0) { // 右指针指向非零
                right++;
                continue;
            }

            swap(nums, left, right);

        }
    }

    public void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    public static void main(String[] args) {
        int[] nums = {1,1,0,3,0,9,12,0};
//        int[] nums = {0,1,0,3,12};
//        int[] nums = {0,1};
        MoveZeroes moveZeroes = new MoveZeroes();
        moveZeroes.moveZeroes(nums);
        System.out.println(nums);
    }

}
